/**
 * Created by hxpeng on 2017/5/11.
 */
(function () {
    'use strict';

    angular.module('App.pages.member.entrust')
        .factory("EntrustService",EntrustService);

    function EntrustService($q,EntrustModel,AppUtils,$timeout,$state) {

        var service = {
            loadEntrustPage: loadEntrustPage,
            loadEntrustInfo: loadEntrustInfo,
            createEntrust: createEntrust,
            deletedEntrust: deletedEntrust
        };

        function loadEntrustPage(params){
            var deferred = $q.defer();
            EntrustModel.getEntrustPage(params).$promise.then(function(response){
                deferred.resolve(response);
            })
            return deferred.promise;
        }

        function loadEntrustInfo(entrustId){
            var deferred = $q.defer();
            EntrustModel.getEntrustInfo({entrustId: entrustId}).$promise.then(function(response){
                if(response.stat == 'err'){
                    AppUtils.showAlert(response.msg);
                    deferred.resolve(null);
                }else{
                    deferred.resolve(response.entrust);
                }
            })
            return deferred.promise;
        }
        function createEntrust(obj){
            var deferred = $q.defer();
            EntrustModel.saveEntrust(obj).$promise.then(function(response){
                if(response.stat == 'err'){
                    AppUtils.showAlert(response.msg);
                }else{
                    AppUtils.showTipsTop("提交成功！");
                    $timeout(function(){
                        $state.go("entrust.parentEntrustList",{},{ reload: true });
                    },100)
                }
                deferred.resolve(null);
            })
            return deferred.promise;
        }
        function deletedEntrust(){

        }

        return service;
    }

})();